from django.db import models
from django.contrib.auth.models import User

from user.manage import UserScoreManage


# Create your models here.

class UserInfo(models.Model):
    """
    头像、 出生日期、 昵称、姓名、性别、  所属用户（关系属性）
    """
    avatar = models.ImageField(upload_to="avatar/",default="avatar/default.png",verbose_name="头像",db_comment="用户头像")

    birthday = models.DateField(null=True,blank=True,verbose_name="出生日期",db_comment="用户出生日期")

    nickname = models.CharField(max_length=32,verbose_name="昵称",db_comment="用户昵称",null=True,blank=True)

    name = models.CharField(max_length=32,verbose_name="姓名",db_comment="用户姓名",null=True,blank=True)

    gender = models.CharField(max_length=8,choices=(("M","男"),("F","女"),("S","保密")),
                              default="S",verbose_name="性别",db_comment="用户性别")

    user = models.OneToOneField(to=User,on_delete=models.CASCADE,
                                null=True,blank=True,related_name="info",
                                verbose_name="所属用户",db_comment="所属用户")

    class Meta:
        db_table = "user_info"


class ScoreConfig(models.Model):
    """
    动作(注册、充值、上传、评论)，  赠送积分数量、 是否启用、 是否删除
    """
    action = models.CharField(max_length=32,verbose_name="动作",db_comment="用户动作",unique=True)

    score = models.IntegerField(verbose_name="赠送积分数量",db_comment="赠送积分数量",default=0)

    is_enable = models.BooleanField(default=True,verbose_name="是否启用",db_comment="是否启用")

    is_delete = models.BooleanField(default=False,verbose_name="是否删除",db_comment="是否删除")

    class Meta:
        db_table = "tb_score_config"
        verbose_name = "积分配置"
        verbose_name_plural = "积分配置"


class Userscore(models.Model):
    """
    用户（关系属性）、积分、 积分来源、获取时间、 资源ID (不用外键关联)
    """
    score = models.IntegerField(verbose_name="积分",db_comment="用户积分",default=0)

    remark = models.CharField(max_length=100,verbose_name="积分来源",db_comment="积分来源")

    create_time = models.DateTimeField(auto_now_add=True,verbose_name="获取时间",db_comment="积分获取时间")

    user = models.ForeignKey(to=User,on_delete=models.CASCADE,verbose_name="所属用户",db_comment="所属用户",
                                null=True,blank=True,related_name="score_list",)

    res_id = models.IntegerField(verbose_name="资源ID",db_comment="资源ID",null=True,blank=True,db_index=True)

    # 添加自定义模型管理器
    objects = UserScoreManage(ScoreConfig)

    class Meta:
        db_table = "tb_user_score"


class UserBlack(models.Model):
    """
    用户（关系属性）、拉黑用户（关系属性）、拉黑时间
    """
    user = models.ForeignKey(to=User,on_delete=models.CASCADE,
                             null=True,blank=True,related_name="black_list",
                             verbose_name="所属用户",db_comment="所属用户")

    black_user = models.ForeignKey(to=User,on_delete=models.CASCADE,
                                   null=True,blank=True,
                                   verbose_name="拉黑用户",db_comment="被拉黑用户")

    create_time = models.DateTimeField(auto_now_add=True,verbose_name="拉黑时间",db_comment="拉黑时间")

    class Meta:
        db_table = "tb_user_black"
        unique_together = ("user","black_user")